//
//  ConfigableViewController.m
//  HZHome
//
//  Created by SunShine on 13-9-22.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ConfigableViewController.h"

@interface ConfigableViewController ()

@end

@implementation ConfigableViewController
@synthesize config = _config;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.navigationItem.leftBarButtonItem = autoavBackButton(self, @selector(backMethod));
}

- (void)viewWillAppear:(BOOL)animated
{
    if (iPhone5) {
        [self.backScrollView setScrollEnabled:NO];
    } else {
        [self.backScrollView setFrame:CGRectMake(0, 64, 320, 416)];
        [self.backScrollView setContentSize:CGSizeMake(320, 504)];
        [self.backScrollView setScrollEnabled:YES];
    }
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"search_bg_v2.png"] forBarMetrics:UIBarMetricsDefault];

}
-(void)backMethod
{
    _onBackMethod = YES;
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)showSearchView
{
    self.searchView = [SearchView initView];
    [self.searchView setFrame:CGRectMake(0, 0, 320, 43)];
    self.searchView.delegate = self;
    [self.view addSubview:self.searchView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIView *)getInputAccessoryView
{
    CGRect accessFrame =  CGRectMake(0.0f, 0.0f, 320.0f, 30.0f);
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:accessFrame];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *buttonFinish = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleBordered target:self action:@selector(inputDidFinish:)];
    NSDictionary *titleAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil];
    [buttonFinish setTitleTextAttributes:titleAttributes forState:UIControlStateNormal];
    [buttonFinish setTitleTextAttributes:titleAttributes forState:UIControlStateHighlighted];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    [toolbar setItems:[NSArray arrayWithObjects:space, buttonFinish, nil]];
    return toolbar;
}

-(void)inputDidFinish:(id)sender
{
    [[[UIApplication sharedApplication].windows objectAtIndex:0] endEditing:YES];
}

-(void)startLoadingView
{
    if (!_loadingView) {
        _loadingView = [[MBProgressHUD alloc]initWithView:self.view];
    }
    _loadingView.labelText = @"正在载入...";
    [self.view addSubview:_loadingView];
    [_loadingView show:YES];
}

-(void)startLoadingViewWithText:(NSString *)text
{
    if (!_loadingView) {
        _loadingView = [[MBProgressHUD alloc]initWithView:self.view];
    }
    if (text && text.length > 0) {
        _loadingView.labelText = text;
    } else {
        _loadingView.labelText = @"正在载入...";
    }
    [self.view addSubview:_loadingView];
    [_loadingView show:YES];
}

-(void)stopLoadingView
{
    [_loadingView show:NO];
    [_loadingView removeFromSuperview];
    _loadingView = nil;
}

-(void)showAlertWithTitle:(NSString *)title Msg:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(CGFloat)caculateHeightForString:(NSString *)string AndFontSize:(CGFloat)size AndWitdh:(CGFloat)width
{
    float fPadding = 16.0; // 8.0px x 2
    if (string == nil) {
        return 0.0;
    }
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string attributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:size]}];
    CGRect textRect = [attributedText boundingRectWithSize:(CGSize){width - fPadding, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    return textRect.size.height + fPadding;//cell的总高度为文本高度+留白
}

-(CGFloat)caculateHeightWithoutPaddingForString:(NSString *)string AndFontSize:(CGFloat)size AndWitdh:(CGFloat)width
{
    if (string == nil || string == (id)[NSNull null]) {
        return 0.0;
    }
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string attributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:size]}];
    CGRect textRect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    return textRect.size.height;
}

@end
